<template>
  <div class="app-container">
    <div class="query-box">
      <el-button type="primary" plain icon="el-icon-circle-plus-outline" size="small" class="mr10" @click="handleAdd">新增</el-button>
    </div>

    <el-table v-loading="loading" :data="accountList" element-loading-text="Loading" border fit class="table">
      <el-table-column align="left" label="编码">
        <template slot-scope="scope">{{ scope.row.code }}</template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value=1 :inactive-value=2 active-color="#409EFF" inactive-color="#B9B9B9" @change="changeStatus(scope.$index, scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | datetimeFormat}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.status === 1 || scope.row.status === 2"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination background layout="total, prev, pager, next" :current-page="query.pageNo"
                     :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
    </div>

    <!-- 添加或修改权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="系统编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入系统编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="系统名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入系统名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="系统状态" prop="type">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in Object.keys(enableStatus)" :label="item">{{enableStatus[item]}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {accounts, addAccount, updateAccount, delAccount, sys} from "@/api/api";

  export default {
    name: "Account",
    data() {
      return {
        enableStatus: window.enableStatus,
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 权限表格树数据
        accountList: [],
        pageTotal: 0,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        query: {
          code: undefined,
          name: undefined,
          pageNo: 0,
          pageSize: 10
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{required: true, message: "权限名称不能为空", trigger: "blur"}],
          code: [{required: true, message: "权限编码不能为空", trigger: "blur"}],
        }
      };
    },
    created() {
      this.getSysList();
    },
    methods: {
      /** 查询权限列表 */
      getSysList() {
        this.loading = true;
        sys().then(response => {
          console.log(response.data)
          this.sysList = response.data;
          this.loading = false;
        });
      },
      // 权限状态字典翻译
      typeFormat(row, column) {
        return window.authType[row.type]
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          code: undefined,
          name: undefined,
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getSysList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.open = true;
        this.title = "添加系统";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "修改系统【" + row.name + "】";
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addSys(this.form).then(response => {
              this.msgSuccess("新增系统成功");
              this.open = false;
              this.getSysList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除名称为【' + row.name + '】的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          delSys(row.code);
        }).then(() => {
          this.getSysList();
          this.msgSuccess("删除成功");
        })
      },
      changeStatus(index, row) {
        updateSysStatus(row.code, row.status == 1 ? "enable" : "disable").catch( res => {
          this.sysList[index].status = row.status == 1 ? 2 : 1
          this.sysList[index].statusName = enableStatus[status]
        });
      },
      // 分页导航
      handlePageChange(val) {
        this.$set(this.query, 'pageNo', val)
        this.getSysList()
      }
    }
  };
</script>
